package com.gzsz.service;

import com.gzsz.util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static java.lang.System.out;

public class WardBedServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException{
        DBUtil dbUtil=new DBUtil();
        out.println("entry -----");
        String method= request.getParameter("method");
        if (method.equals("delete")){
            String bed_id= request.getParameter("bed_id");
            String delete_sql="delete from ward_bed where bed_id="+bed_id+";";
            if (dbUtil.executeUpdateBySQL(delete_sql)>0){
                resp.sendRedirect("html1/ward-bed.jsp");
            }else {
                out.println("删除失败");
            }
        }
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException{
        String method= request.getParameter("method");
        DBUtil dbUtil=new DBUtil();
        if (method.equals("add")){
            //1.获取表单的值
            String ward_id = request.getParameter("ward_id");
            String bed_number = request.getParameter("bed_number");
            String bed_status = request.getParameter("bed_status");
            //2.拼接SQL语句执行数据库
            String add_sql="INSERT INTO ward_bed (ward_id, bed_number, bed_status) " +
                    "VALUES ('"+ward_id+"', '"+bed_number+"', '"+bed_status+"');";
            if (dbUtil.executeUpdateBySQL(add_sql)>0){
                resp.sendRedirect("html1/ward-bed.jsp");
            }else {
                out.println("添加失败");
            }
        } else if (method.equals("edit")) {
            //1.获取表单的值
            String bed_id = request.getParameter("bed_id");
            String ward_id = request.getParameter("ward_id");
            String bed_number = request.getParameter("bed_number");
            String bed_status = request.getParameter("bed_status");
            //2.拼接SQL语句执行数据库
            String edit_sql="UPDATE ward_bed SET bed_id='"+bed_id+"',ward_id='"+ward_id+"',bed_number='"+bed_number+"',bed_status='"+bed_status+"'where bed_id='"+bed_id+"'";

            if (dbUtil.executeUpdateBySQL(edit_sql)>0){
//                System.out.println("编辑成功");
                resp.sendRedirect("html1/ward-bed.jsp");
            }else {
                out.println("编辑失败");
            }
        }
    }
}




